<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-alert title="添加商品消息" type="info" :closable="false" center> </el-alert>
      <!-- 步骤条:active就是当前活动的步骤条索引 接收的式number类型-->
      <el-steps :active="activeStep - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="基本属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- 标签页中的v-model 绑定的就是当前点击的name值 -->
      <el-form :model="addGoodsForm" :rules="addGoodsrules" ref="ruleForm" label-width="100px" class="demo-ruleForm" label-position="top">
        <el-tabs v-model="activeStep" tab-position="left" @tab-click="tabClick">
          <el-tab-pane label="基本信息" name="0" :before-leave="beforLeaveTags">
            <!-- 表单内容区域 -->
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addGoodsForm.goods_name" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model.number="addGoodsForm.goods_price" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model.number="addGoodsForm.goods_weight" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model.number="addGoodsForm.goods_number" size="mini"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <!-- 级联选中器 -->
              <el-cascader v-model="addGoodsForm.goods_cat" :options="cateList" :props="catelable" @change="handleChange"></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-form-item v-for="item in activeData" :key="item.attr_id" :label="item.attr_name">
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox v-for="(val, i) in item.attr_vals" :key="i" :label="val" border></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="基本属性" name="2">
            <el-form-item v-for="item in stateData" :key="item.attr_id" :label="item.attr_name">
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <!-- upLoad组件 on-preview预览图片时触发-->
            <el-upload :action="upLoadURL" :on-preview="handlePreview" :on-remove="handleRemove" list-type="picture" :headers="headersObj">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="addGoodsForm.qleditorcontent"> </quill-editor>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'addgoods',
  data() {
    return {
      activeStep: 0, //表示当前激活的步骤
      addGoodsForm: {
        //绑定给表单用来接收用户输入将来发送给服务器的
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_cat: [],
        qleditorcontent:''
      },
      //表单校验规则
      addGoodsrules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }], //非空校验
        goods_price: [{ required: true, message: '请输入商品名称', trigger: 'blur' }], //非空校验
        goods_number: [{ required: true, message: '请输入商品名称', trigger: 'blur' }], //非空校验
        goods_weight: [{ required: true, message: '请输入商品名称', trigger: 'blur' }], //非空校验
      },
      cateList: [],
      //级联选择器的配置项
      catelable: {
        label: 'cat_name', //展示的名称
        value: 'cat_id', //该名称对应的值
        children: 'children', //子分类
        expandTrigger: 'hower', //触发的模式
      },
      activeData: [], //动态参数
      stateData: [], //静态属性
      checkList: '',
      upLoadURL: 'http://www.tangxiaoyang.vip:8888/api/v2/upload', //图片上传路径
      headersObj: {
        Authorization: JSON.parse(sessionStorage.getItem('userInfo')).token,
      }, //图片上传时的请求头
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败！')
      }
      this.cateList = res.data
    },
    handleChange() {
      if (this.addGoodsForm.goods_cat.length !== 3) {
        this.addGoodsForm.goods_cat = []
      }
    },
    //切换标签页时触发
    beforLeaveTags() {
      if (this.addGoodsForm.goods_cat.length !== 3) {
        this.$message.error('请选择商品分类')
        return false
      }
    },
    async tabClick() {
      if (this.activeStep == 1 && this.cateId) {
        //表示选择了商品参数标签页
        const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
          params: { sel: 'many' },
        })
        if (res.meta.status !== 200 && this.cateId) {
          return this.$message.error('数据获取失败！')
        }
        res.data.forEach((item) => {
          item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
        })
        this.activeData = res.data
        console.log(this.activeData)
      } else if (this.activeStep == 2) {
        //表示选择了商品参数标签页
        const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
          params: { sel: 'only' },
        })
        if (res.meta.status !== 200) {
          return this.$message.error('数据获取失败！')
        }
        this.stateData = res.data
        console.log(this.stateData)
      }
    },
    //图片预览时触发
    handlePreview() {},
    //移除图片时触发
    handleRemove() {},
  },
  computed: {
    cateId() {
      return this.addGoodsForm.goods_cat.length == 3 ? this.addGoodsForm.goods_cat[2] : null
    },
  },
}
</script>

<style lang="less" scoped>
.el-checkbox {
  margin-right: 10px;
}
.ql-editor{
    min-height: 300px !important;
}
.ql-blank{
    min-height: 300px !important;
}
</style>